<template>
	<view class="content fofa poRel">
		<tabnav title="电子签" :col="back"></tabnav>
		<view style="height: 180rpx;"></view>
		<view class="dis disAl disJuC">
			<image src="https://www.sign.run/applet/static/seal/farenzhang.png" style="width: 180rpx;height: 180rpx;"></image>
		</view>
		<view class="textCen foSi40 fowe600 mar-top20">电子签</view>
		<view style="height: 100rpx;"></view>
		<view class="wid90 mar dis foSi28" style="color: #878686;align-items: flex-start;" @click="isTrue = !isTrue">
			<checkbox class="red hei100" style="transform:scale(0.7);" :checked="isTrue"></checkbox>
			<view style="line-height: 50rpx;">我已阅读并同意<span :style="'color: '+back+';'" @click.stop="tanOne = true">《用户协议》</span>。</view>
		</view>
		<view style="height: 60rpx;"></view>
		<view v-if="isTrue" class="wid100 hei80">
			<button class="wid90 mar hei100" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
				<view class="wh100 borRad10 dis disAl disJuC coFFF foSi30" :style="'background-color: '+back+';'">手机号一键登录</view>
			</button>
		</view>
		<view v-if="!isTrue" class="wid100 hei80">
			<view class="wid90 mar hei100 borRad10 dis disAl disJuC coFFF foSi30" @click="tishi"
			:style="'background-color: '+back+';'">手机号一键登录</view>
		</view>
	</view>
</template>

<script>
	import tabnav from "../common/tabnav/tabnav.vue"
	export default {
		components: {
			tabnav,
		},
		data() {
			return {
				back: uni.getStorageSync('back'),
				isTrue: false,
			}
		},
		onLoad() {
		},
		created() {
			var that = this
			uni.getSystemInfo({
				success: (e) => {
					console.log(e)
					that.tops = e.statusBarHeight
				}
			})
		},
		methods: {
			tiaozhuan(url){
				uni.navigateTo({
					url: url
				});
			},
			tishi(){
				uni.showToast({
					title: "请勾选用户协议",
					icon: 'error'
				})
			},
			getPhoneNumber (e) {
				console.log(e)
				
				var that = this
				uni.showLoading({
					title:'加载中...'
				})
				uni.login({
				  success: function (loginRes) {
				    console.log(loginRes);
					uni.request({
						url: that.global.url+'api/qianyu_dzq/applet_getphone',
						method: 'GET',
						header:{'site-id':that.global.site_id},
						data: {
						    code:e.detail.code,
							openidcode:loginRes.code
						},
						success(res) {
						    let result = res.data
						    if(result.code == 1){
						       that.login(result.data)
						    }else{
						        uni.showToast({
						            title: result.msg,
						            icon:'none'
						        })
						    }
						},
						complete() {
							uni.hideLoading()
						}
					})
				  }
				});
				// this.login(18143079335)
			},
			getFlowId(id){
				let that = this
				uni.request({
					url: that.global.url+'api/qianyu_dzq/applet_get_flowid_to_did',
					method: 'POST',
					header:{'site-id':that.global.site_id,'token': uni.getStorageSync('inuse_userinfo_token')},
					data: {id: id},
					success(res) {
					    let result = res.data
					    if(result.code == 1){
							console.log(result.data)
							that.getRecipient(result.data.flowId)
					    }else{
					        uni.showToast({
					            title: result.msg,
					            icon:'none',
								duration: 2000
					        })
					    }
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			getRecipient(flowId){
				let that = this
				uni.request({
					url: that.global.url+'api/qianyu_dzq/applet_bind_recipient',
					method: 'POST',
					header:{'site-id':that.global.site_id,'token': uni.getStorageSync('inuse_userinfo_token')},
					data: {flowid: flowId},
					success(res) {
					    let result = res.data
					    if(result.code == 1){
							console.log(result.data)
							uni.navigateBack()
					    }else{
							console.log('失败')
					        uni.showToast({
					            title: result.msg,
					            icon:'none',
								duration: 2000
					        })
							uni.navigateBack()
					    }
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			login(mobile){
				// 用户使用流程分主账号和使用中，也就是个人版和多个企业版，其中以个人版为主，部分接口需要使用个人版所以要存两个用户信息，注意区分
				var that = this
				uni.showLoading({
					title:'加载中...'
				})
				
				uni.request({
					url: that.global.url+'api/qianyu_dzq/applet_userlogin',
					method: 'GET',
					header:{'site-id':that.global.site_id},
					data: {
					    phone:mobile
					},
					success(res) {
					    let result = res.data
					    if(result.code == 1){
					        // 存用户信息
					        uni.setStorageSync('main_userinfo', result.data) // 主账号用户信息
							uni.setStorageSync('main_userinfo_token', result.data.token) // 主账号token
							// 存使用中账号信息，由于刚刚登录所以使用中就是主账号，同步存
							uni.setStorageSync('inuse_userinfo', result.data)   // 使用中的用户信息
							uni.setStorageSync('inuse_userinfo_token', result.data.token) // 使用中的用户token
							uni.$emit('Refresh_userinfo',{msg:'更新用户信息重新获取数据'})
							var scene = uni.getStorageSync('scene');
							if(scene){
								that.getFlowId(scene)
							}else{
								uni.navigateBack()
							}
					    }else{
					        uni.showToast({
					            title: result.msg,
					            icon:'none'
					          })
					    }
					},complete() {
						uni.hideLoading()
					}
				})
			}
		}
	}
</script>

<style>
	.content {}
	.xia{
		height: 1rpx;
		background: #eaeaea;
	}
	button::after {
		border: none;
	}
	button {
		display: block;
		margin-left: auto;
		margin-right: auto;
		padding-left: 0px;
		padding-right: 0px;
		box-sizing: border-box;
		text-align: center;
		text-decoration: none;
		-webkit-tap-highlight-color: transparent;
		overflow: hidden;
		background-color: #fff;
	}
	checkbox.red[checked] .wx-checkbox-input,
	checkbox.red.checked .uni-checkbox-input{
		background-color: #035DFF !important;
		border-color: #035DFF !important;
		color: #ffffff !important;
	}
</style>